﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using GAM_Web_Designer_Pro.Properties;
using ColorRichTextBox.ColorRichTextBox;

namespace GAM_Web_Designer_Pro {
    public static class SchemaAccess {
        private static string _dataDir = Application.StartupPath + Settings.Default.SchemeDir;

        /// <summary>
        /// Saves the schema.
        /// </summary>
        /// <param name="scheme">The schema to save</param>
        /// <param name="path">The path where to save</param>
        public static void SaveSchema( clsColorRichTextBoxSchema schema, string path ) {
            try {
                
                // Check to see if the path has the schemas extension.
                if ( !path.EndsWith( Settings.Default.SchemeExt ) )
                    path += Settings.Default.SchemeExt;

                // Open a stream to the path so we can write to it.
                FileStream stream = File.OpenWrite( path );
                BinaryFormatter formatter = new BinaryFormatter();
                // Serialize the project to save it.
                formatter.Serialize( stream, schema );
                // Close the stream.
                stream.Close();
            }
            catch ( Exception e ) {
                new BugReporter.BugReporterForm(Application.ProductName,
                    Application.ProductVersion, e.ToString(), e.Message).Show();
            }
        }

        /// <summary>
        /// Gets all the schemas in the scheme directory.
        /// </summary>
        public static SortedList<string, clsColorRichTextBoxSchema> LoadAllSchemas() {
            SortedList<string, clsColorRichTextBoxSchema> schemes = new SortedList<string, clsColorRichTextBoxSchema>();

            try {

                // Get a list of all the files in the schemes data directory.
                string[] filesInDir = Directory.GetFiles( _dataDir );
                FileStream stream;
                clsColorRichTextBoxSchema scheme;
                BinaryFormatter formatter = new BinaryFormatter();

                // Now well go through each file and check if
                // it has a extension matching our scheme files 
                // extension and if it does well add it to the list.
                for ( int i = 0; i < filesInDir.Length; i++ ) {
                    if ( filesInDir[i].EndsWith( Settings.Default.SchemeExt ) ) {
                        stream = File.OpenRead( filesInDir[ i ] );
                        scheme = (clsColorRichTextBoxSchema)formatter.Deserialize( stream );
                        stream.Close();
                        schemes.Add( scheme.SchemeType.ToString(), scheme );
                    }
                }
                return schemes;
            }
            catch ( Exception e ) {
                new BugReporter.BugReporterForm(Application.ProductName,
                    Application.ProductVersion, e.ToString(), e.Message).Show();
            }

            return schemes;
        }

        public static clsColorRichTextBoxSchema LoadSchema( string path ) {

            clsColorRichTextBoxSchema scheme = new clsColorRichTextBoxSchema( clsColorRichTextBoxSchema.enDefaultColorSchemaTypes.Other );
            
            try {

                // Get a list of all the files in the schemes data directory.
                FileStream stream = File.Open( path, FileMode.Open );
                BinaryFormatter formatter = new BinaryFormatter();
                
                scheme = (clsColorRichTextBoxSchema)formatter.Deserialize( stream );
                stream.Close();

                return scheme;
            }
            catch ( Exception e ) {
                new BugReporter.BugReporterForm( Application.ProductName,
                    Application.ProductVersion, e.ToString(), e.Message ).Show();
            }

            return scheme;
        }
    }
}
